package com.zxx.coolweather._wuguan2;

import android.content.Context;
import android.content.DialogInterface;
import android.widget.Toast;

import com.yanzhenjie.nohttp.error.NetworkError;
import com.yanzhenjie.nohttp.error.NotFoundCacheError;
import com.yanzhenjie.nohttp.error.ServerError;
import com.yanzhenjie.nohttp.error.TimeoutError;
import com.yanzhenjie.nohttp.error.URLError;
import com.yanzhenjie.nohttp.error.UnKnownHostError;
import com.yanzhenjie.nohttp.rest.OnResponseListener;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.Response;
import com.zxx.coolweather.MyApplication;

/**
 * @author zxx on 2022/1/12
 */
public class ResponseListener<T> implements OnResponseListener<T> {

    private Request<T> mRequest;
    private WaitDialog mDialog;
    private HttpCallBack<T> callBack;
    private boolean isShowError;

    public ResponseListener(final Request<T> mRequest, Context context, HttpCallBack<T> callBack, boolean isShowDialog, boolean isCanCancel, boolean isShowError) {
        this.mRequest = mRequest;
        this.callBack = callBack;
        this.isShowError = isShowError;

        if (context != null && isShowDialog) {
            mDialog = new WaitDialog(context);
            mDialog.setCancelable(isCanCancel);

            mDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {
                    mRequest.cancel();
                }
            });
        }
    }

    @Override
    public void onStart(int what) {
        if (mDialog != null && !mDialog.isShowing()) {
            mDialog.show();
        }
    }

    @Override
    public void onSucceed(int what, Response<T> response) {
        if (callBack != null) {
            callBack.onSucceed(what, response);
        }
    }

    @Override
    public void onFailed(int what, Response<T> response) {
        if (isShowError) {
            Exception exception = response.getException();
            if (exception instanceof ServerError) {
                //Toast.show("服务器发生错误");
                show("服务器发生错误");
            } else if (exception instanceof NetworkError) {
                show("请检查网络");
            } else if (exception instanceof TimeoutError) {// 请求超时
                show("请求超时，网络不好或者服务器不稳定");
            } else if (exception instanceof UnKnownHostError) {// 找不到服务器
                show("未发现指定服务器");
            } else if (exception instanceof URLError) {// URL是错的
                show("URL错误");
            } else if (exception instanceof NotFoundCacheError) {
                show("没有发现缓存");
            } else {
                show("未知错误");
            }
        }
        if (callBack != null) {
            callBack.onFailed(what, response.getHeaders().getETag(), response.getTag(), response.getException(), response.responseCode(), response.getNetworkMillis());
        }

    }

    @Override
    public void onFinish(int what) {
        if (mDialog != null && mDialog.isShowing()) {
            mDialog.dismiss();
        }
    }


    public void show(String msg) {
        Toast.makeText(MyApplication.getContext(), msg, Toast.LENGTH_SHORT).show();
    }
}
